package com.zdlyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdlyh.domain.ResponseResult;
import com.zdlyh.domain.dto.MenuDto;
import com.zdlyh.domain.vo.MenuVo1;
import com.zdlyh.domain.entity.Menu;
import com.zdlyh.domain.entity.User;
import com.zdlyh.service.MenuService;
import com.zdlyh.mapper.MenuMapper;
import com.zdlyh.service.UserService;
import com.zdlyh.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{
    @Autowired
    private UserService userService;

    @Override
    public List<String> getPermsByUserId(Long userId) {
        // 判断是否为管理员，不是则返回所具有的权限
        User user = userService.getById(userId);
        if (user.getType().equals("1")){
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Menu::getStatus,0)
                    .in(Menu::getMenuType,"C","F");
            List<Menu> menuList = baseMapper.selectList(queryWrapper);

            List<String> perms = menuList.stream()
                    .map(Menu::getPerms)
                    .collect(Collectors.toList());
            return perms;
        }
        return baseMapper.getPermsByUserId(userId);
    }

    @Override
    public List<Menu> getRouterMenuTreeByUserId(Long userId) {
        // 判断是否为管理员，不是则返回所具有的menu
        User user = userService.getById(userId);
        List<Menu> menus;
        if (user.getType().equals("1")){
            menus = baseMapper.getAllRouterMenu();
        }else {
            menus = baseMapper.getRouterMenuTreeByUserId(userId);
        }
        // 构建tree
        // 先找出第一层的菜单，然后再找他们的子菜单设置到children属性中
        List<Menu> menusTree = builderMenuTree(menus,0L);
        return menusTree;
    }

    @Override
    public List<MenuVo1> getMenuList(MenuDto menuDto) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(menuDto.getStatus()),Menu::getStatus,menuDto.getStatus())
                .like(StringUtils.hasText(menuDto.getMenuName()),Menu::getMenuName,menuDto.getMenuName())
                .orderByAsc(Menu::getId).orderByAsc(Menu::getOrderNum);
        List<Menu> menuList = baseMapper.selectList(queryWrapper);

        List<MenuVo1> menuVo1s = BeanCopyUtils.copyBeanList(menuList, MenuVo1.class);
        return menuVo1s;
    }

    private List<Menu> builderMenuTree(List<Menu> menus, Long parentId) {
        List<Menu> menuTree = menus.stream()
                // 过滤
                .filter(menu -> menu.getParentId().equals(parentId))
                .map(menu -> menu.setChildren(getChildren(menu, menus)))
                .collect(Collectors.toList());
        return menuTree;
    }

    // 获取传入参数的子菜单
    private List<Menu> getChildren(Menu menu, List<Menu> menus) {
        List<Menu> childrenList = menus.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))
                // 如果有三级继续调归递用
                .map(m->m.setChildren(getChildren(m,menus)))
                .collect(Collectors.toList());
        return childrenList;
    }

    @Override
    public List<Long> selectMenuListByRoleId(Long roleId) {
        return baseMapper.selectMenuListByRoleId(roleId);
    }

    @Override
    public ResponseResult addMenu(Menu menu) {
        baseMapper.insert(menu);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getMenuById(Long id) {
        Menu menu = baseMapper.selectById(id);
        MenuVo1 menuVo1 = BeanCopyUtils.copyBean(menu, MenuVo1.class);
        return ResponseResult.okResult(menuVo1);
    }

    @Override
    public ResponseResult updateMenu(Menu menu) {
        if (menu.getParentId().equals(menu.getId())){
            return ResponseResult.errorResult(500,"修改菜单"+menu.getMenuName()+"失败，上级菜单不能选择自己");
        }
        baseMapper.updateById(menu);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteMenuById(Long id) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId,id);
        if (count(queryWrapper) != 0){
            return ResponseResult.errorResult(500,"存在子菜单不允许删除");
        }
        baseMapper.deleteById(id);
        return ResponseResult.okResult();
    }
}




